class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        resulthead=result=ListNode(0)
        while l1 is not None or l2 is not None :
            temp=l1.val if l1 is not None else 0
            temp+=l2.val if l2 is not None else 0
            result.val+=temp;
            if result.val>=10:
                result.next=ListNode(1)
                result.val=result.val-10
            elif l1.next is not None or l2.next is not None:
                result.next=ListNode(0)
            l1=l1.next if l1 is not None else None
            l2=l2.next if l2 is not None else None
            result=result.next
        return resulthead